package com.wwcom.darteagle.data;

import com.wwcom.darteagle.model.Match;
import com.wwcom.darteagle.model.Score;
import com.wwcom.darteagle.model.VisitScore;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;
import android.util.Log;

public class VisitScoreObject extends DataObject {
    private static final String TABLE_NAME = "VISITSCORE";
    private static final String TABLE_CREATE =
            "CREATE TABLE " + TABLE_NAME + " (" +
            "VISITSCORE_ID integer primary key autoincrement," +
            "MATCH_ID integer not null," +
            "VISITSCORE_ORDER integer not null," +
            "PLAYER_NAME text," +
            "VISITSCORE text);";
    
    public VisitScoreObject(SQLiteDatabase database) {
    	super(database);
    }
    
	static public String createSQL() {
		return TABLE_CREATE;
	}

	static public String deleteSQL() {
		return "DROP TABLE IF EXISTS "+TABLE_NAME;
	}	
	
    static public String upgradeSQL(int oldVersion, int newVersion) {
    	return null;
    }
    
    public long insert(VisitScore vs, long matchId, int visitScoreOrder) {
        ContentValues contentValues = new ContentValues();
    	contentValues.put("MATCH_ID", matchId);
    	contentValues.put("PLAYER_NAME", vs.getMatchPlayer().getPlayer().getName());
    	contentValues.put("VISITSCORE_ORDER", visitScoreOrder);    	
    	String visitScore= Integer.toString(vs.getVisitScore());
    	int[] dartScore= vs.getUnvalidatedScore().getDartScore();
    	boolean[] isDouble= vs.getUnvalidatedScore().getIsDouble();
    	boolean[] isTreble= vs.getUnvalidatedScore().getIsTreble();
    	visitScore+= ","+Integer.toString(dartScore[0])+","+Integer.toString(dartScore[1])+","+Integer.toString(dartScore[2]);
    	visitScore+= ","+Boolean.toString(isDouble[0])+","+Boolean.toString(isDouble[1])+","+Boolean.toString(isDouble[2]);
    	visitScore+= ","+Boolean.toString(isTreble[0])+","+Boolean.toString(isTreble[1])+","+Boolean.toString(isTreble[2]);
    	contentValues.put("VISITSCORE", visitScore );
    	return database.insert(TABLE_NAME, null, contentValues);
    	
    }
    
    
    public void getAll(Match match, long matchId) {
    	Cursor cursor = database.query(TABLE_NAME, 
    			new String[] {"MATCH_ID","PLAYER_NAME","VISITSCORE","VISITSCORE_ORDER"}, 
    			"MATCH_ID="+matchId, null, null, null,"VISITSCORE_ORDER");
        if(cursor.getCount() >0) {
            while (cursor.moveToNext()) {
                 String visitScore = cursor.getString(cursor.getColumnIndex("VISITSCORE"));
                 String[] vs= TextUtils.split(visitScore, ",");
                 int[] dartScores= new int[3];
                 dartScores[0]= Integer.parseInt(vs[1]);
                 dartScores[1]= Integer.parseInt(vs[2]);
                 dartScores[2]= Integer.parseInt(vs[3]);
                 boolean[] isDouble= new boolean[3];
                 isDouble[0]= Boolean.parseBoolean(vs[4]);
                 isDouble[1]= Boolean.parseBoolean(vs[5]);
                 isDouble[2]= Boolean.parseBoolean(vs[6]);
                 boolean[] isTreble= new boolean[3];
                 isTreble[0]= Boolean.parseBoolean(vs[7]);
                 isTreble[1]= Boolean.parseBoolean(vs[8]);
                 isTreble[2]= Boolean.parseBoolean(vs[9]);
                 Score s= new Score(dartScores,isDouble,isTreble);
                 match.updateCurrentPlayerScore(s);
                 match.nextPlayer();
                 if (match.isMatchComplete()) {  //don't advance to next leg or set if match is complete
                	 Log.d("VisitScoreObject","match complete");
                	 Log.d("VisitScoreObject","match leg target - "+match.getNumLegs());
                	 Log.d("VisitScoreObject","match leg number - "+match.getLegNumber());
                	 break;
                 } else if (match.isSetComplete()) {
                	 Log.d("VisitScoreObject","set complete");
                	 match.nextSet();
                 } else if (match.isLegComplete()) {
                	 Log.d("VisitScoreObject","leg complete");
                	 match.nextLeg();
                 }
            }
        }
    }
    
    public void deleteAll(long matchId) {
    	database.delete(TABLE_NAME, "MATCH_ID=?", new String[] { Long.toString(matchId) } );
    }
       
    
}
